package com.gitee.huxing.course.netty.gateway.router;

import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.net.URI;

/**
 * @Description: 基础路由处理
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.netty.gateway.router
 * @ClassName: BaseNettyRouter
 * @Author: huxing
 * @DateTime: 2021-08-21 下午2:19
 */
@Slf4j
@ConditionalOnMissingBean(name = "router")
public class NettyRouter extends AbstractNettyRouter{

    @Override
    public HttpRequest router(HttpRequest request) throws Exception {
        String uri = request.uri();
        log.info("收到业务请求: {}", uri);
        // 进行如有路由选择判断
        if (uri.contains("/api/v1/")){
            request.headers().set("host", "127.0.0.1");
            request.headers().set("port", "8088");
        } else if (uri.contains("/api/v2/")) {
            request.headers().set("host", "127.0.0.1");
            request.headers().set("port", "8089");
        }
        return request;
    }
}
